#include "mixtureofgaussian.h"

MixtureOfGaussian::MixtureOfGaussian() :
    Processor(), NbClasses(3) , classeMeans(3), classeVars(3), classeWeights(3)
{
    unsigned int n(0);
    for(n=0 ; n < NbClasses ; n++)
    {
        classeMeans[n] = NULL;
        classeVars[n]  = NULL;
    }
}

MixtureOfGaussian::~MixtureOfGaussian()
{
    unsigned int n(0);
    for(n=0 ; n < NbClasses ; n++)
        if( classeMeans[n] )
            cvReleaseImage(&classeMeans[n]);
    for(n=0 ; n < NbClasses ; n++)
        if( classeVars[n] )
            cvReleaseImage(&classeVars[n]);
    for(n=0 ; n < NbClasses ; n++)
        if( classeWeights[n] )
            cvReleaseImage(&classeWeights[n]);
}

MixtureOfGaussian::MixtureOfGaussian(const MixtureOfGaussian &mog) :
    Processor(mog), NbClasses(mog.NbClasses), classeMeans(mog.NbClasses), classeVars(mog.NbClasses), classeWeights(mog.NbClasses)
{
    unsigned int n(0);
    for(n=0 ; n < NbClasses ; n++)
    {
        classeMeans[n]    = NULL;
        classeVars[n]     = NULL;
        classeWeights[n]  = NULL;
    }
}

MixtureOfGaussian& MixtureOfGaussian::operator=(const MixtureOfGaussian &mog)
{
    classeMeans.resize(mog.NbClasses);
    classeVars.resize(mog.NbClasses);
    classeWeights.resize(mog.NbClasses);
    unsigned int n(0);
    for(n=0 ; n < NbClasses ; n++)
    {
        classeMeans[n]    = NULL;
        classeVars[n]     = NULL;
        classeWeights[n]  = NULL;
    }
    NbClasses = mog.NbClasses;
    return (*this);
}


IplImage * MixtureOfGaussian::init  (IplImage *frame)
{
    unsigned int n(0);
    for(n=0 ; n < NbClasses ; n++)
    {
        classeMeans[n]    = cvCreateImage(cvSize(frame->width,frame->height), 32, 3);
        classeVars[n]     = cvCreateImage(cvSize(frame->width,frame->height), 32, 3);
        classeWeights[n]  = cvCreateImage(cvSize(frame->width,frame->height), 32, 1);
        cvConvertScale(frame, classeMeans[n], 1.0/255.0);
        cvSet(classeWeights[n],cvRealScalar(0.01));
        cvSet(classeWeights[n],cvRealScalar(1.0/((double)(NbClasses))));
    }

    alpha = 0.05;

    return frame;
}


IplImage * MixtureOfGaussian::update(IplImage *frame)
{


    return classeWeights[1];
}
